//
//  Singleton.m
//  TourPlay
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "Singleton.h"

@implementation Singleton
+ (instancetype)shareSingleton
{
    static Singleton *singleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[Singleton alloc] init];
    });
    return singleton;
}
// 打开数据库
-(void)openDB
{
    //导入sqlite框架，导入FMDB文件夹
    
    //1.获得数据库文件的路径
    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName=[doc stringByAppendingPathComponent:@"TourPlay.sqlite"];
    NSLog(@"%@", fileName);
    
    //2.获得数据库
    self.db = [FMDatabase databaseWithPath:fileName];
    
    //3.打开数据库
    if ([self.db open]) {
        NSLog(@"打开数据库成功!");
    } else {
         NSLog(@"打开数据库失败!");
    }
}
- (void)exeWithSQL:(NSString *)sql
{
    BOOL result = [self.db executeUpdate:sql];
    
    if (result) {
//        NSLog(@"创表成功");
    }else{
        NSLog(@"创表失败");
    }
}
- (NSMutableArray *)selectChooseTypeWithSQL:(NSString *)sql
{
    NSMutableArray *arr = [NSMutableArray array];
    //1.执行查询语句
    FMResultSet *resultSet = [self.db executeQuery:sql];
    //2.遍历结果集合
    while ([resultSet next]) {
        NSString *Id = [resultSet objectForColumnName:@"Id"];
        NSString *isTrue = [resultSet objectForColumnName:@"isTrue"];
        NSString *name = [resultSet objectForColumnName:@"name"];
        
        ChooseTypeMod *chooseType = [[ChooseTypeMod alloc] init];
        chooseType.Id = Id;
        chooseType.isTrue = isTrue;
        chooseType.name = name;
        [arr addObject:chooseType];
        [chooseType release];
    }
    return arr;
}
- (NSMutableArray *)selectRecordActivityWithSQL:(NSString *)sql
{
    NSMutableArray *arr = [NSMutableArray array];
    //1.执行查询语句
    FMResultSet *resultSet = [self.db executeQuery:sql];
    //2.遍历结果集合
    while ([resultSet next]) {
        NSString *Id = [resultSet objectForColumnName:@"Id"];
        NSString *time = [resultSet objectForColumnName:@"time"];
        NSString *title = [resultSet objectForColumnName:@"title"];
        
 
        RecordAcitvityMod *record = [[RecordAcitvityMod alloc] init];
        record.Id = Id;
        record.time = time;
        record.title = title;
        [arr addObject:record];
        [record release];
    }
    return arr;
}
@end
